<?php
elgg_load_library('elgg:class_organization');

function check_avatar($entity)
{
	// Now see if we have a file icon
	if ((isset($_FILES['icon'])) && (substr_count($_FILES['icon']['type'],'image/'))) {
		
		$icon_sizes = elgg_get_config('icon_sizes');
		$prefix = "organizations/".$entity->guid;
	
		$filehandler = new ElggFile();
		$filehandler->owner_guid = $entity->owner_guid;
		$filehandler->setFilename($prefix . ".jpg");
		$filehandler->open("write");
		$filehandler->write(get_uploaded_file('icon'));
		$filehandler->close();
	
		$thumbtiny = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['tiny']['w'], $icon_sizes['tiny']['h'], $icon_sizes['tiny']['square']);
		$thumbsmall = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['small']['w'], $icon_sizes['small']['h'], $icon_sizes['small']['square']);
		$thumbmedium = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['medium']['w'], $icon_sizes['medium']['h'], $icon_sizes['medium']['square']);
		$thumblarge = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['large']['w'], $icon_sizes['large']['h'], $icon_sizes['large']['square']);

		if ($thumbtiny)
		{
			$thumb = new ElggFile();
			$thumb->owner_guid = $entity->owner_guid;
			$thumb->setMimeType('image/jpeg');
	
			$thumb->setFilename($prefix."tiny.jpg");
			$thumb->open("write");
			$thumb->write($thumbtiny);
			$thumb->close();
	
			$thumb->setFilename($prefix."small.jpg");
			$thumb->open("write");
			$thumb->write($thumbsmall);
			$thumb->close();
	
			$thumb->setFilename($prefix."medium.jpg");
			$thumb->open("write");
			$thumb->write($thumbmedium);
			$thumb->close();
	
			$thumb->setFilename($prefix."large.jpg");
			$thumb->open("write");
			$thumb->write($thumblarge);
			$thumb->close();
	
			$entity->setMetaData('icontime', time());
		}
	}
}

//Manage the request data
$guid 			= get_input('guid', '');
$name 			= get_input('name', '');
$description 	= get_input('description', '');
$address 		= get_input('address', '');
$phone 			= get_input('phone', '');
$web 			= get_input('web', '');
$cif 			= get_input('cif', '');

if($name)
{
	//TODO: Check the name (unique)
	$object 			= new Organization($guid);
	$object->access_id 	= 2;
	$object->title 		= $name;
	$object->description= $description;
	$object->address 	= $address;
	$object->phone 		= $phone;
	$object->web 		= $web;
	$object->cif 		= $cif;
	
	if($guid && $object->save())
	{
		check_avatar($object);		
		system_message(elgg_echo('organizations:edition:success'));
		
	}
	else if($object->save())
	{
		check_avatar($object);
		system_message(elgg_echo('organizations:adding:success'));
	}
	else
		register_error(elgg_echo('organizations:saving:fail'));
	
	forward(REFERER);
}
else 
{
	register_error(elgg_echo('organizations:saving:need_name'));
}



//$field = get_input('input_field_name', 'default_value');
//forward(get_loggedin_user()->getURL());
//system_message('Your message');



?>